import {
  Controller,
  Get,
  UseGuards,
  Req,
  Param,
  Post,
  Body,
  Delete,
  Patch,
} from "@nestjs/common";
import { JwtAuthGuard } from "../auth/guards/jwt-auth.guard";
import { ProductService } from "../product/product.service";
import { RespWrap } from "src/common/base.utils";
import { UpdateProductDto } from "./dto/update.product.dto";
import { CreateProductDto } from "./dto/create.product.dto";
import {ModuleEntity} from "../module/module.entity";
import { getRepository } from "typeorm";
import {ProductEntity} from "./product.entity";

@UseGuards(JwtAuthGuard)
@Controller("products")
export class ProductController {
  constructor(private productService: ProductService) {}


  @Get(':id')
  async get(@Param() params) {
    return RespWrap(await getRepository(ProductEntity).findOne({id: params.id}));
  }

  @Get()
  async pageQuery(@Req() req) {
    req.query.userId = req.user.id;
    const resp = await this.productService.pageQuery({
      ...req.query,
    });
    return RespWrap(resp);
  }

  @Post()
  async insert(@Body() body: CreateProductDto, @Req() req) {
    body.userId = req.user.id;
    const resp = await this.productService.insert(body);
    return RespWrap(resp);
  }

  @Delete(":id")
  async removeById(@Param() params) {
    const resp = await this.productService.removeById(params.id);
    return RespWrap(resp);
  }

  @Patch(":id")
  async update(@Param() params, @Body() body: UpdateProductDto) {
    const resp = await this.productService.update({
      id: params.id,
      ...body,
    });
    return RespWrap(resp);
  }
}
